package cn.edu.njtc.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 给前端返回结果的统一的封装类型
 *
 */

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public static <T> R<T> ok(T t) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMsg("success");
        r.setData(t);
        return r;
    }

    public static <T> R<T> ok(String msg) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> failed() {
        R<T> r = new R<>();
        r.setCode(500);
        r.setMsg("filed");
        return r;
    }

    public static <T> R<T> failed(String msg) {
        R<T> r = new R<>();
        r.setCode(500);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> failed(Integer code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
}
